﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using Manul.Genetics.Genom;
using Manul.Genetics.Compiler;

namespace Manul.Genetics.Character
{
    [XmlInclude(typeof(Character))]
    public class Character : CharacterItem
    {
        public Character()
        {
            IsLethal = false;
            IsCompiled = false;

            Params.AllowNew = true;
            Params.AllowRemove = true;
            Params.RaiseListChangedEvents = true;
            Params.AllowEdit = true;
        }

        public bool IsLethal
        { get; set; }

        public String Code
        { get; set; }

        public List<String> EpistasisGuid = new List<String>();
        
        [XmlIgnore]
        public List<Character> Epistasis = new List<Character>();

        [XmlIgnore]
        public List<Gen> Gens = new List<Gen>();

        [XmlIgnore]
        public bool IsCompiled
        { get; set; }

        [XmlIgnore]
        public List<GenomCompilationError> CompilationErrors = new List<GenomCompilationError>();

        public void UpdateEpistasisGuid()
        {
            EpistasisGuid.Clear();
            foreach (Character epistasisItem in Epistasis)
                EpistasisGuid.Add(epistasisItem.GUID);
        }

        public void LoadEpistasisByGuid(CharacterDirectory directoryItems)
        {
            int i = 0;
            while (i < EpistasisGuid.Count)
            {
                NamedItem item = directoryItems.FindByGuid(EpistasisGuid[i]);
                if (item != null && item is Character)
                    Epistasis.Add((Character)item);
                else
                    EpistasisGuid.RemoveAt(i);
                i++;
            }
        }

        public BindingList<PropertyValue> Params = new BindingList<PropertyValue>();

        public bool CheckLoop(Character newCharacter)
        {
            return false;
        }

        #region ICompilable Members

        public override List<CompilationResult> Compile(GenomData genomDataItem)
        {
            return GenomCompiler.Compile(genomDataItem, this);
        }

        #endregion
    }
}
